package com.neu.carbon.mes.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.neu.carbon.mes.mapper.PlanMapper;
import com.neu.carbon.mes.mapper.ScheduledMapper;
import com.neu.carbon.wms.domain.WmsMaterialInfo;
import com.neu.carbon.wms.mapper.WmsMaterialInfoMapper;
import com.neu.common.enums.SerialNoPrefix;
import com.neu.common.utils.DateUtils;
import com.neu.common.utils.SecurityUtils;
import com.neu.smty.utils.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neu.carbon.mes.domain.MesMaterialRequisitionDetail;
import com.neu.carbon.mes.mapper.MesMaterialRequisitionMapper;
import com.neu.carbon.mes.domain.MesMaterialRequisition;
import com.neu.carbon.mes.service.IMesMaterialRequisitionService;

/**
 * 领料单Service业务层处理
 * 
 * @author neuedu
 * @date 2024-11-24
 */
@Service
public class MesMaterialRequisitionServiceImpl implements IMesMaterialRequisitionService 
{
    @Autowired
    private MesMaterialRequisitionMapper mesMaterialRequisitionMapper;

    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;

    @Autowired
    private ScheduledMapper scheduledMapper;
    @Autowired
    private PlanMapper planMapper;
    /**
     * 查询领料单
     * 
     * @param id 领料单ID
     * @return 领料单
     */
    @Override
    public MesMaterialRequisition selectMesMaterialRequisitionById(Long id)
    {
        MesMaterialRequisition mesMaterialRequisition = mesMaterialRequisitionMapper.selectMesMaterialRequisitionById(id);
        if (mesMaterialRequisition!=null){
            Long productId = mesMaterialRequisition.getProductId();
            if(productId!=null){
                WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(productId);
                if(materialInfo!=null){
                    String name = materialInfo.getName();
                    String unit = materialInfo.getUnit();
                    String specification = materialInfo.getSpecification();
                    String model = materialInfo.getModel();

                    if(name!=null){
                        mesMaterialRequisition.setMaterialName(name);
                    }
                    if(unit!=null){
                        mesMaterialRequisition.setUnit(unit);
                    }
                    if(specification!=null){
                        mesMaterialRequisition.setSpecification(specification);
                    }
                    if(model!=null){
                        mesMaterialRequisition.setModel(model);
                    }

                    List<MesMaterialRequisitionDetail> detailList = mesMaterialRequisition.getMesMaterialRequisitionDetailList();
                    if(detailList!=null){
                        for (MesMaterialRequisitionDetail mesMaterialRequisitionDetail : detailList) {
                            Long materialId = mesMaterialRequisitionDetail.getMaterialId();
                            if(materialId!=null){
                                WmsMaterialInfo m = wmsMaterialInfoMapper.selectWmsMaterialInfoById(materialId);
                                String name1 = m.getName();
                                mesMaterialRequisitionDetail.setMaterialName(name1);

                                String unit1 = materialInfo.getUnit();
                                String specification1 = materialInfo.getSpecification();
                                String model1 = materialInfo.getModel();
                                if(unit1!=null){
                                    mesMaterialRequisitionDetail.setUnit(unit1);
                                }
                                if(specification1!=null){
                                    mesMaterialRequisitionDetail.setSpecification(specification1);
                                }
                                if(model1!=null){
                                    mesMaterialRequisitionDetail.setModel(model1);
                                }
                            }
                        }
                    }

                    Long scheduleId = mesMaterialRequisition.getScheduleId();
                    if(scheduleId!=null){
                        String no = scheduledMapper.selectNoById(scheduleId);
                        BigDecimal a = scheduledMapper.selectQuantityById(scheduleId);
                        Date date = scheduledMapper.selectDateById(scheduleId);
                        if (no!=null){
                            mesMaterialRequisition.setScheduleNo(no);
                        }
                        if(a!=null){
                            mesMaterialRequisition.setRequireQuantity(a);
                        }
                        if(date!=null){
                            mesMaterialRequisition.setProductDate(date);
                        }
                    }

                }
            }

            Long planId = mesMaterialRequisition.getPlanId();
            if(planId!=null){
                String string = planMapper.selectNoById(planId);
                mesMaterialRequisition.setPlanNo(string);
            }

        }
        return mesMaterialRequisition;
    }

    /**
     * 查询领料单列表
     * 
     * @param mesMaterialRequisition 领料单
     * @return 领料单
     */
    @Override
    public List<MesMaterialRequisition> selectMesMaterialRequisitionList(MesMaterialRequisition mesMaterialRequisition)
    {
        List<MesMaterialRequisition> list = mesMaterialRequisitionMapper.selectMesMaterialRequisitionList(mesMaterialRequisition);
        if(list!=null){
            for (MesMaterialRequisition materialRequisition : list) {
                Long productId = materialRequisition.getProductId();
                if(productId!=null){
                    WmsMaterialInfo materialInfo = wmsMaterialInfoMapper.selectWmsMaterialInfoById(productId);
                    String unit = materialInfo.getUnit();
                    String specification = materialInfo.getSpecification();
                    String model = materialInfo.getModel();

                    if(unit!=null){
                        materialRequisition.setUnit(unit);
                    }
                    if(specification!=null){
                        materialRequisition.setSpecification(specification);
                    }
                    if(model!=null){
                        materialRequisition.setModel(model);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 新增领料单
     * 
     * @param mesMaterialRequisition 领料单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesMaterialRequisition(MesMaterialRequisition mesMaterialRequisition)
    {
        mesMaterialRequisition.setSerialNo(CodeUtil.getSerialNo(SerialNoPrefix.LLSQ));
        mesMaterialRequisition.setApplyTime(DateUtils.getNowDate());
        mesMaterialRequisition.setApplyUser(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));

        int rows = mesMaterialRequisitionMapper.insertMesMaterialRequisition(mesMaterialRequisition);
        insertMesMaterialRequisitionDetail(mesMaterialRequisition);
        return rows;
    }

    /**
     * 修改领料单
     * 
     * @param mesMaterialRequisition 领料单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesMaterialRequisition(MesMaterialRequisition mesMaterialRequisition)
    {
        mesMaterialRequisitionMapper.deleteMesMaterialRequisitionDetailByMaterialRequisitionId(mesMaterialRequisition.getId());
        insertMesMaterialRequisitionDetail(mesMaterialRequisition);
        return mesMaterialRequisitionMapper.updateMesMaterialRequisition(mesMaterialRequisition);
    }

    /**
     * 批量删除领料单
     * 
     * @param ids 需要删除的领料单ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialRequisitionByIds(Long[] ids)
    {
        mesMaterialRequisitionMapper.deleteMesMaterialRequisitionDetailByMaterialRequisitionIds(ids);
        return mesMaterialRequisitionMapper.deleteMesMaterialRequisitionByIds(ids);
    }

    /**
     * 删除领料单信息
     * 
     * @param id 领料单ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMaterialRequisitionById(Long id)
    {
        mesMaterialRequisitionMapper.deleteMesMaterialRequisitionDetailByMaterialRequisitionId(id);
        return mesMaterialRequisitionMapper.deleteMesMaterialRequisitionById(id);
    }

    /**
     * 新增领料单物料明细信息
     * 
     * @param mesMaterialRequisition 领料单对象
     */
    public void insertMesMaterialRequisitionDetail(MesMaterialRequisition mesMaterialRequisition)
    {
        List<MesMaterialRequisitionDetail> mesMaterialRequisitionDetailList = mesMaterialRequisition.getMesMaterialRequisitionDetailList();
        Long id = mesMaterialRequisition.getId();
        if (StringUtils.isNotNull(mesMaterialRequisitionDetailList))
        {
            List<MesMaterialRequisitionDetail> list = new ArrayList<MesMaterialRequisitionDetail>();
            for (MesMaterialRequisitionDetail mesMaterialRequisitionDetail : mesMaterialRequisitionDetailList)
            {
                mesMaterialRequisitionDetail.setMaterialRequisitionId(id);
                list.add(mesMaterialRequisitionDetail);
            }
            if (list.size() > 0)
            {
                mesMaterialRequisitionMapper.batchMesMaterialRequisitionDetail(list);
            }
            List<MesMaterialRequisition> mesMaterialRequisitions = mesMaterialRequisitionMapper.selectMesMaterialRequisitionList(mesMaterialRequisition);
            System.out.println(mesMaterialRequisitions);
        }
    }
}
